﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net;
using System.IO;
using System.Web;
using System.Runtime.Serialization;

namespace cn.loozo.BrokenArrow
{

    class Websites
    {
        private const string HOUR_RATE_URL = "http://click.loozo.cn/api/hourrate";
        private const string WEBSITE_URL = "http://click.loozo.cn/api/website";

        private List<Website> ws;
        private SortedList hr = new SortedList();
        int lastDay = -1 ;
        /*
        *每天获取新的刷站信息
         */
        public void init()
        {
            if (lastDay != new DateTime().DayOfYear)
            {
                // 从服务器获取小时分布和点击数据

                jsonHR(SendRequest(HOUR_RATE_URL));

                jsonWebsite(SendRequest(WEBSITE_URL));

                lastDay = new DateTime().DayOfYear;
            }
        }
        /*
         *获得当前可刷的网站
         */
        public List<Website> getCurrentWebsite()
        {
            int rate = (int)hr[new DateTime().Hour];
            List<Website> result = new List<Website>();
            foreach (Website element in ws)
            {
                if (element.hit < (element.total / 100 * rate)) {
                    result.Add(element);
                }
            }
            return result;
        }


        public void jsonHR(string jsonstr)   { 
            SortedList hourList = new SortedList();

            System.Runtime.Serialization.Json.DataContractJsonSerializer _Json = 
                new System.Runtime.Serialization.Json.DataContractJsonSerializer(hourList.GetType());
            byte[] _Using = System.Text.Encoding.UTF8.GetBytes(jsonstr);
            System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(_Using);
            _MemoryStream.Position = 0;

            hr = (SortedList)_Json.ReadObject(_MemoryStream);
        }

        public void jsonWebsite(string jsonstr)
        {
            List<Website> websiteList = new List<Website>();

            System.Runtime.Serialization.Json.DataContractJsonSerializer _Json =
                new System.Runtime.Serialization.Json.DataContractJsonSerializer(websiteList.GetType());
            byte[] _Using = System.Text.Encoding.UTF8.GetBytes(jsonstr);
            System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(_Using);
            _MemoryStream.Position = 0;

            ws = (List<Website>)_Json.ReadObject(_MemoryStream);
        }

        /********************  
        *C# HTTP Request请求程序模拟***  
         * 向服务器送出请求  
         * */
        public string SendRequest(string url)
        {
            HttpWebRequest request =
            (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            if (response.StatusCode.ToString() != "OK")
            {
                return response.StatusDescription.ToString();
            }

            StreamReader Reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);

            return Reader.ReadToEnd();
        }


        public List<Website> WebSite
        {
            get { return ws; }
            set { ws = value; }
        }

        public SortedList HourRate
        {
            get { return hr; }
            set { hr = value; }
        }
    }

    public class Website
    {
        public string referrer = "";
        public string link = "";
        public string ad = "";
        public int total = 100;
        public int hit = 0;
    } 
}
